package ouyuwang.gpsbox;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.GnssStatus;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import java.util.Iterator;

/**
 * Created by yupeng on 17/07/20.
 */

public class GPStatusHandler implements GpsStatus.Listener {

    private static final String TAG = "GpsActivity";

    private Main activity = null;

    public GPStatusHandler(Main activity) {
        this.activity = activity;
    }

    @Override
    public void onGpsStatusChanged(int event) {
        switch (event) {
            // 第一次定位
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                activity.updateMessage("第一次定位");
                break;
            // 卫星状态改变
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                activity.updateMessage("卫星状态改变");
                // 获取当前状态
                GpsStatus gpsStatus = activity.getLocationManager().getGpsStatus(null);
                // 获取卫星颗数的默认最大值
                int maxSatellites = gpsStatus.getMaxSatellites();
                // 创建一个迭代器保存所有卫星
                Iterator<GpsSatellite> iters = gpsStatus.getSatellites()
                        .iterator();
                int count = 0;
                while (iters.hasNext() && count <= maxSatellites) {
                    GpsSatellite s = iters.next();
                    count++;
                }
                activity.updateMessage("搜索到：" + count + "颗卫星");
                break;
            // 定位启动
            case GpsStatus.GPS_EVENT_STARTED:
                activity.updateMessage("定位启动");
                break;
            // 定位结束
            case GpsStatus.GPS_EVENT_STOPPED:
                activity.updateMessage("定位结束");
                break;
        }
    }
}
